from google.appengine.ext import webapp
from support import render_page, play_cutscene, get_room_id, get_path, get_game
from core.menu import Menu
from datetime import datetime

class BaseHandler(webapp.RequestHandler):
    
    # returns template to be used to render room.
    # can be overridden for specific rooms
    def get_template(self):
        return 'room.html'
    
    def get(self):
        self.update('get')
    
    def post(self):
        self.update('post')
    
    # handles both get and post requests
    def update(self,request_type):
        self.time = datetime.time(datetime.now())
        
        # get game object, which holds the current state of the game
        self.game = get_game()
        
        this_room = self.get_this_room()
        
        player = self.game.get_player()
        
        # build menu for room and player
        menu = Menu()
        menu.build_menu(this_room, player)
        
        # if user clicked on an action button we let the
        # corresponding action object update the game state
        # and then redirect to action's destination if it has one
        # or else back to this page
        action_name = self.request.get("action_name")
        if action_name:
            action = menu.actions.get(action_name)
            if action:
                action.do_action(self.game,this_room,self.request)
                self.game.update()
                if action.is_nav_action():
                    self.redirect(get_path(action.get_destination()))
                else:
                    self.redirect(self.request.path)
                return
        
        # if we reach this point in the code, user did not click on an action
        # or the action object couldn't be found (hopefully that doesn't happen)
        # next step is to update inventory if requested
        inventory_request = self.request.get("inventory")
        if inventory_request:
            self.update_inventory(this_room,player)
            
        # final step is to render the page for the current room (if this is a get)
        if request_type == 'get':
            web_page_parameters = {
               "this_page":self.request.path,
               "room":this_room,
               "menu":menu,
               "player":player,
               "cutscene":play_cutscene(),
           }
            
            web_page = render_page(self.get_template(),web_page_parameters)
            self.response.out.write(web_page)
            
        # otherwise this is a post and we need to redirect back to this page (as get)
        else:
            self.redirect(self.request.path)
    
    
    def get_this_room(self):
        
        # get id of room (based on handler's module name)
        room_id = get_room_id(self)
        
        # get class to use for new room if we need one
        room_class = self.get_room_class()
        
        this_room = self.game.get_room(room_id,room_class)
        
        return this_room

    # update player and room inventories (called from past handler for each room)
    def update_inventory(self,room,player):
        
        # get name of items that user checked
        add_stuff = self.request.get_all("room_inventory")
        drop_stuff = self.request.get_all("player_inventory")
        
        # note:  in what follows we remove item first, so we get an object
        # and then we add that object to the other list
        
        # if user picked something up, then update player and room inventory
        if add_stuff:
            for item_name in add_stuff:
                item = room.pop_item(item_name)
                player.add_item(item)
            self.game.update()
        
        # if user dropped something, then update player and room inventory
        if drop_stuff:
            for item_name in drop_stuff:
                item = player.pop_item(item_name)
                room.add_item(item)
            self.game.update()
            
        return
